//
//  JXHHomeCell.swift
//  JXHSinaWeiBo
//
//  Created by juxiaohui on 16/10/24.
//  Copyright © 2016年 juxiaohui. All rights reserved.
//

import UIKit
import SDWebImage
import HYLabel

private let edgeMargin : CGFloat = 15
private let picMargin  : CGFloat = 10

class JXHHomeCell: UITableViewCell {

    // MARK: - 属性
    
    @IBOutlet weak var contentLabelWConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var picViewHConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var retweetLabelTopConstraint: NSLayoutConstraint!
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var verifiedImageView: UIImageView!
    
    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var picViewWConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var picViewBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var vipImageView: UIImageView!
    
    @IBOutlet weak var timeLabel: UILabel!
    
    @IBOutlet weak var sourceLabel: UILabel!
    
    @IBOutlet weak var contentLabel: HYLabel!
    
    @IBOutlet weak var retweetLabel: HYLabel!
    
    @IBOutlet weak var picCollectionView: JXHCustomCollectionView!
    
    @IBOutlet weak var retweetView: UIView!
    
    @IBOutlet weak var bottomToolView: UIView!
    var viewModel : JXHStatusViewModel? {
        didSet{
            guard let viewModel = viewModel else {
                return
            }
            iconImageView.sd_setImage(with: viewModel.iconUrl, placeholderImage: UIImage.init(named: "avatar_default_small"))
            verifiedImageView.image = viewModel.verifiedImage
            nameLabel.text = viewModel.status?.user?.screen_name
            vipImageView.image = viewModel.mbrankImage
            timeLabel.text = viewModel.created_atText
            sourceLabel.text = viewModel.sourceText
            
            contentLabel.attributedText = JXHFindEmoticon.shareInstance.findAttrString(statusText: viewModel.status?.text, font: contentLabel.font)
            //contentLabel.text = viewModel.status?.text
            nameLabel.textColor = viewModel.verifiedImage == nil ? UIColor.orange : UIColor.black
            
            let picSize = calculatePicViewSize(count: viewModel.picURLs.count)
            picViewHConstraint.constant = picSize.height
            picViewWConstraint.constant = picSize.width
            picCollectionView.pics = viewModel.picURLs
            
            if viewModel.status?.retweeted_status?.text != nil {
                
                if let screenName = viewModel.status?.retweeted_status?.user?.screen_name,let retweetText = viewModel.status?.retweeted_status?.text  {
                    retweetLabel.text = "@" + "\(screenName): " + retweetText
                    
                    retweetLabelTopConstraint.constant = 15
                }
                
                retweetView.isHidden = false
                
            }else{
                retweetLabel.text = nil
                retweetView.isHidden = true
                retweetLabelTopConstraint.constant = 0
            }
            //计算cell高度
            //强制布局
            if viewModel.cellHeght == 0 {
                
                layoutIfNeeded()
                viewModel.cellHeght = bottomToolView.frame.maxY
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
     
        contentLabelWConstraint.constant = UIScreen.main.bounds.width - 2 * edgeMargin
        iconImageView.layer.cornerRadius = 20.0
        iconImageView.layer.masksToBounds = true
    }
}

// MARK: - 就算PicView的size
extension JXHHomeCell {
 
    func calculatePicViewSize(count : Int) -> CGSize {
        //没有配图
        
        if count == 0 {
            
            picViewBottomConstraint.constant = 0
            return CGSize.init()
        }
        
        picViewBottomConstraint.constant  = 10
        /// 设置pic的布局
        if count == 1 {
            
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewModel?.picURLs.first?.absoluteString)
            
            let flowLayout = picCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
            flowLayout.itemSize = CGSize.init(width: (image?.size.width)! * 2, height: (image?.size.height)! * 2)
            
            return CGSize.init(width: (image?.size.width)! * 2, height: (image?.size.height)! * 2)
        }
        
        let imageWH = (UIScreen.main.bounds.width - 2 * edgeMargin - 2 * picMargin) / 3
         let flowLayout = picCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = CGSize.init(width: imageWH, height: imageWH)
        
        
        //四张
        if count == 4 {
            let picWH = imageWH * 2 + picMargin + 0.1
            return CGSize.init(width: picWH, height: picWH)
        }
        
        //其他配图
        
        let rows =  CGFloat((count - 1) / 3  + 1)
        let picViewH = rows * imageWH + (rows - 1) * picMargin
        let picViewW = UIScreen.main.bounds.width - 2 * edgeMargin

        
       // (count -1) / 每行个数  + 1 = rows
        
        return CGSize.init(width: picViewW, height: picViewH)
    }
}
